package com.aiju.cash.ui.pay;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.event.BlotterEvent;
import com.aiju.cash.ui.billing.BillingActivity;
import com.aiju.cash.ui.main.MainActivity;
import com.aiju.cash.ui.pay.manager.PayManager;
import com.aiju.cash.ui.pay.manager.PrintManager;
import com.aiju.common.event.EventCenter;
import com.aiju.utils.PreferencesUtils;

/**
 * @author: 佐为
 * @date: 2016-12-06 14:40
 * @version: V1.0
 * @Description: 支付成功
 */
public class PaySuccessActivity extends BaseTitleActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_success);
        setCenterTitle("收款成功");

        String money = getIntent().getStringExtra("money");
        ((TextView) findViewById(R.id.tv_pay_success)).setText(money);

        if (PayManager.isOrderFrom()) {
            final String serialNum = getIntent().getStringExtra("serialNum");
            boolean autoPrint = PreferencesUtils.getBoolean(this, "autoPrint");
            if (autoPrint) {//如果自动打印
                print(serialNum);
            }
            //手动打印
            findViewById(R.id.btn_pay_order).setVisibility(View.VISIBLE);
            findViewById(R.id.btn_pay_order).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    print(serialNum);
                }
            });

            findViewById(R.id.btn_pay_go).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(PaySuccessActivity.this, BillingActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);

                    EventCenter.getInstance().post(new BlotterEvent());  //通知有一笔流水计入
                }
            });
        } else {
            findViewById(R.id.btn_pay_order).setVisibility(View.VISIBLE);
            findViewById(R.id.btn_pay_go).setVisibility(View.GONE);
            ((TextView) findViewById(R.id.btn_pay_order)).setText("继续收款");

            findViewById(R.id.btn_pay_order).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(PaySuccessActivity.this, PayActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);

                    EventCenter.getInstance().post(new BlotterEvent());  //通知有一笔流水计入
                }
            });
        }
    }

    //打印订单
    private void print(final String serialNum) {
        PrintManager.getInstance().printReceipt(PaySuccessActivity.this, serialNum, new PrintManager.PrintCallBack() {
            @Override
            public void onPrintSuccess() {
                Intent intent = new Intent(PaySuccessActivity.this, BillingActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }

            @Override
            public void onPrintFailure(String msg) {
                Intent intent = new Intent(PaySuccessActivity.this, PrintErrorActivity.class);
                intent.putExtra("serialNum", serialNum);
                intent.putExtra("msg", msg);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(PaySuccessActivity.this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        EventCenter.getInstance().post(new BlotterEvent()); //通知有一笔流水计入
    }
}
